package top.blackr.folivora.common.core.utils;

import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.experimental.UtilityClass;
import top.blackr.folivora.common.core.R;
import top.blackr.folivora.common.core.enums.ResultEnum;

import java.io.IOException;

@UtilityClass
public class RequestUtil {

    public <T> void responseR(HttpServletResponse response, R<T> r) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try {
            response.getWriter().write(JSONUtil.toJsonStr(r));
            response.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void responseEnum(HttpServletResponse response, ResultEnum resultEnum) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try {
            response.getWriter().write(JSONUtil.toJsonStr(R.failed(resultEnum)));
            response.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void responseEnum(HttpServletResponse response, String msg) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try {
            response.getWriter().write(JSONUtil.toJsonStr(R.failed(msg)));
            response.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
